/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.XtSuspendApply;
import com.thinkgem.jeesite.modules.ta.service.StSuspendService;
import com.thinkgem.jeesite.modules.ta.service.XtSuspendApplyService;

/**
 * 案件挂起Controller
 * 
 * @author TTAO
 * @version 2017-06-09
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/xtSuspendApply")
public class XtSuspendApplyController extends BaseController {

	@Autowired
	private XtSuspendApplyService xtSuspendApplyService;

	@Autowired
	private StSuspendService stSuspendService;

	@ModelAttribute
	public XtSuspendApply get(@RequestParam(required = false) String id) {
		XtSuspendApply entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = xtSuspendApplyService.get(id);
		}
		if (entity == null) {
			entity = new XtSuspendApply();
		}
		return entity;
	}

	@RequestMapping(value = { "list", "" })
	public String list(XtSuspendApply xtSuspendApply,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<XtSuspendApply> page = xtSuspendApplyService.findPage(
				new Page<XtSuspendApply>(request, response), xtSuspendApply);
		model.addAttribute("page", page);
		return "modules/ta/xtSuspendApplyList";
	}

	@RequiresPermissions("ta:xtSuspendApply:view")
	@RequestMapping(value = "form")
	public String form(XtSuspendApply xtSuspendApply, Model model) {
		model.addAttribute("xtSuspendApply", xtSuspendApply);
		return "modules/ta/xtSuspendApplyForm";
	}

	public String ascii2native(String ascii) {
		int n = ascii.length() / 6;
		StringBuilder sb = new StringBuilder(n);
		for (int i = 0, j = 2; i < n; i++, j += 6) {
			String code = ascii.substring(j, j + 4);
			char ch = (char) Integer.parseInt(code, 16);
			sb.append(ch);
		}
		return sb.toString();
	}

	@RequestMapping(value = "toAdd")
	public String toAdd(String iid, String name, ModelMap model) {
		model.put("iid", iid);
		model.put("name", ascii2native(name));
		model.put("uname", UserUtils.getUser().getName());
		return "modules/ta/xtSuspendApplyForm";
	}

	@RequestMapping(value = "save")
	public String save(XtSuspendApply xtSuspendApply, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, xtSuspendApply)) {
			return form(xtSuspendApply, model);
		}
		xtSuspendApply.setUserid(UserUtils.getUser().getNo());
		xtSuspendApply.setApplyTime(new Date());
		xtSuspendApply.setIsdelete(Long.parseLong("1"));
		xtSuspendApplyService.save(xtSuspendApply);
		addMessage(redirectAttributes, "保存案件挂起成功");
		// return "redirect:" + Global.getAdminPath()
		// + "/ta/stSuspend/saveStSuspendByApply?iid="
		// + xtSuspendApply.getSerial().toString();
		return "redirect:" + Global.getAdminPath() + "/ta/xtSuspendApply/list";
	}

	@RequestMapping(value = "update")
	public String update(XtSuspendApply xtSuspendApply, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, xtSuspendApply)) {
			return form(xtSuspendApply, model);
		}
		xtSuspendApply.setIsdelete(Long.parseLong("2"));
		xtSuspendApply.setApplyTime(new Date());
		xtSuspendApplyService.save(xtSuspendApply);
		addMessage(redirectAttributes, "此案件挂起不通过");
		return "redirect:" + Global.getAdminPath() + "/ta/xtSuspendApply/list";
	}

	@RequestMapping(value = "esc")
	public String esc(XtSuspendApply xtSuspendApply, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, xtSuspendApply)) {
			return form(xtSuspendApply, model);
		}
		xtSuspendApply.setIsdelete(Long.parseLong("3"));
		xtSuspendApplyService.save(xtSuspendApply);
		addMessage(redirectAttributes, "已取消挂起此案件");
		return "redirect:" + Global.getAdminPath()
				+ "/ta/workListForWorklistNode/list";
	}

	@RequiresPermissions("ta:xtSuspendApply:edit")
	@RequestMapping(value = "delete")
	public String delete(XtSuspendApply xtSuspendApply,
			RedirectAttributes redirectAttributes) {
		xtSuspendApplyService.delete(xtSuspendApply);
		addMessage(redirectAttributes, "删除案件挂起成功");
		return "redirect:" + Global.getAdminPath()
				+ "/ta/xtSuspendApply/?repage";
	}

}